package com.api.zszsj2.ZYT.kq.reportForms.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.api.zszsj2.ZYT.kq.reportForms.service.KQReportFormsService;
import com.api.zszsj2.ZYT.kq.reportForms.service.impl.KQReportFormsServiceImpl;
import com.api.zszsj2.ZYT.util.ExcelUtils;
import com.api.zszsj2.ZYT.util.JsonResult;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.util.ExcelUtil;
import com.engine.workplan.service.WorkPlanCalendarService;
import com.engine.workplan.service.impl.WorkPlanCalendarServiceImpl;
import com.mzlion.core.lang.StringUtils;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import io.swagger.annotations.ApiOperation;
import jdk.internal.instrumentation.TypeMapping;
import lombok.extern.flogger.Flogger;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpHeaders;
import org.apache.poi.util.IOUtils;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/qk_reportForms")
@Log4j
public class KQReportFormsController extends BaseBean {

    private KQReportFormsService getService(User var1) {
        return ServiceUtil.getService(KQReportFormsServiceImpl.class, var1);
    }

    private WorkPlanCalendarService getService2(User var1) {
        return (WorkPlanCalendarService) ServiceUtil.getService(WorkPlanCalendarServiceImpl.class, var1);
    }

    @GET
    @Path("/get_search_criteria")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult getSearchCriteria(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        String type = Util.null2String(request.getParameter("type"));
        Map searchdepartids = JSON.parseObject("{\"index\":1,\"title\":\"部门\",\"type\":\"browser\",\"dataType\":\"departments\",\"key\":\"departid\"}", Map.class);
        Map searchsubids = JSON.parseObject("{\"index\":2,\"title\":\"分部\",\"type\":\"browser\",\"dataType\":\"subcompanys\",\"key\":\"subid\"}", Map.class);
        Map searchUserids = JSON.parseObject("{\"index\":3,\"title\":\"名字\",\"type\":\"browser\",\"dataType\":\"resources\",\"key\":\"userid\"}", Map.class);
        Map searchYear = JSON.parseObject("{\"index\":4,\"title\":\"年份\",\"type\":\"dataYear\",\"key\":\"year\"}", Map.class);
        Map searchDateRange = JSON.parseObject("{\"index\":5,\"title\":\"时间范围\",\"type\":\"dateRange\",\"key\":\"\",\"child\":[{\"index\":0,\"title\":\"开始时间\",\"type\":\"\",\"key\":\"begindate\"},{\"index\":1,\"title\":\"结束时间\",\"type\":\"\",\"key\":\"enddate\"}]}", Map.class);
        List<Map> list = new ArrayList<>();
        list.add(searchdepartids);
        list.add(searchsubids);
        list.add(searchUserids);
        list.add(searchYear);
        if ("2".equals(type) || "3".equals(type) || "5".equals(type)) {
            list.add(searchDateRange);
        }
        return JsonResult.ok(list);
    }

    /**
     * 请休假统计表数据
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/leave_statistics")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult LeaveStatistics(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subid")));
        param.put("departmentid", Util.null2String(request.getParameter("departid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("begindate", Util.null2String(request.getParameter("begindate")));
        param.put("enddate", Util.null2String(request.getParameter("enddate")));
        param.put("currentPage", Util.null2String(request.getParameter("currentPage")));
        param.put("pageSize", Util.null2String(request.getParameter("pageSize")));
        String type = Util.null2String(request.getParameter("type"));

        Map resMap = new HashMap();
        if ("1".equals(type)) {    //请休假统计表数据
            resMap = getService(user).tableKQReportForms(param, user);
        } else if ("2".equals(type)) {
            resMap = getService(user).tableKQReportFormsLeaveDetail(param, user);  //请休假明细表数据
        } else if ("3".equals(type)) {
            resMap = getService(user).tableKQReportFormsBackDetail(param, user);  //销假明细表数据
        } else if ("4".equals(type)) {
            resMap = getService(user).tableKQReportFormsOverTime(param, user);  //加班统计表数据
        } else if ("5".equals(type)) {
            resMap = getService(user).tableKQReportFormsOverTimeDetail(param, user);  //加班明细数据
        } else {
            return JsonResult.errorMsg("缺少类型参数!");
        }


        return JsonResult.ok(resMap);
    }


    /**
     * 请休假明细表数据
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/leave_detail")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult LeaveDetail(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subcompanyid")));
        param.put("departmentid", Util.null2String(request.getParameter("departmentid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("begindate", Util.null2String(request.getParameter("begindate")));
        param.put("enddate", Util.null2String(request.getParameter("enddate")));
        param.put("currentPage", Util.null2String(request.getParameter("currentPage")));
        param.put("pageSize", Util.null2String(request.getParameter("pageSize")));

        Map resMap = getService(user).tableKQReportFormsLeaveDetail(param, user);
        return JsonResult.ok(resMap);
    }

    /**
     * 销假明细表数据
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/back_detail")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult backDetail(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subcompanyid")));
        param.put("departmentid", Util.null2String(request.getParameter("departmentid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("begindate", Util.null2String(request.getParameter("begindate")));
        param.put("enddate", Util.null2String(request.getParameter("enddate")));
        param.put("currentPage", Util.null2String(request.getParameter("currentPage")));
        param.put("pageSize", Util.null2String(request.getParameter("pageSize")));

        Map resMap = getService(user).tableKQReportFormsBackDetail(param, user);
        return JsonResult.ok(resMap);
    }


    /**
     * 加班统计表数据
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/overtime_statistics")   //details
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult overtimeStatistics(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subcompanyid")));
        param.put("departmentid", Util.null2String(request.getParameter("departmentid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("lastname", Util.null2String(request.getParameter("lastname")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("currentPage", Util.null2String(request.getParameter("currentPage")));
        param.put("pageSize", Util.null2String(request.getParameter("pageSize")));

        Map resMap = getService(user).tableKQReportFormsOverTime(param, user);
        return JsonResult.ok(resMap);
    }


    /**
     * 加班明细数据
     *
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/overtime_detail")   //details
    @Produces(MediaType.APPLICATION_JSON)
    public JsonResult overtimeDetail(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subcompanyid")));
        param.put("departmentid", Util.null2String(request.getParameter("departmentid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("lastname", Util.null2String(request.getParameter("lastname")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("begindate", Util.null2String(request.getParameter("begindate")));
        param.put("enddate", Util.null2String(request.getParameter("enddate")));
        param.put("currentPage", Util.null2String(request.getParameter("currentPage")));
        param.put("pageSize", Util.null2String(request.getParameter("pageSize")));

        Map resMap = getService(user).tableKQReportFormsOverTimeDetail(param, user);
        return JsonResult.ok(resMap);
    }

    @GET
    @Path("/export")
    @Produces({"application/octet-stream"})
    public Response export(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        //获取用户信息
        User user = HrmUserVarify.getUser(request, response);
        //获取参数
        Map param = new HashMap();
        param.put("subcompanyid", Util.null2String(request.getParameter("subcompanyid")));
        param.put("departmentid", Util.null2String(request.getParameter("departmentid")));
        param.put("userid", Util.null2String(request.getParameter("userid")));
        param.put("lastname", Util.null2String(request.getParameter("lastname")));
        param.put("year", Util.null2String(request.getParameter("year")));
        param.put("begindate", Util.null2String(request.getParameter("begindate")));
        param.put("enddate", Util.null2String(request.getParameter("enddate")));
        param.put("currentPage", "0");
        param.put("pageSize", "999999999");

        String type = Util.null2String(request.getParameter("type"));

        Map resMap = new HashMap();
        if ("1".equals(type)) {    //请休假统计表数据
            resMap = getService(user).tableKQReportForms(param, user);
        } else if ("2".equals(type)) {
            resMap = getService(user).tableKQReportFormsLeaveDetail(param, user);  //请休假明细表数据
        } else if ("3".equals(type)) {
            resMap = getService(user).tableKQReportFormsBackDetail(param, user);  //销假明细表数据
        } else if ("4".equals(type)) {
            resMap = getService(user).tableKQReportFormsOverTime(param, user);  //加班统计表数据
        } else if ("5".equals(type)) {
            resMap = getService(user).tableKQReportFormsOverTimeDetail(param, user);  //加班明细数据
        } else {
            return null;
        }
        JSONArray headerArray = JSONArray.parseArray(JSON.toJSONString(resMap.get("headers")));
        String fieldsName = ExcelUtils.traverseTree(headerArray, "", "");
        List<Map> mapList = JSON.parseObject(JSON.toJSONString(resMap.get("pageBean"))).getJSONArray("list").toJavaList(Map.class);

        try {
            String fileName = "报表导出_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss")) + ".xlsx";
            InputStream inputStream = ExcelUtils.export(mapList, fieldsName.substring(0, fieldsName.lastIndexOf(",")), "yyyy-MM-dd HH:mm");
            return Response.ok(inputStream).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
